---@class IReactiveProxy 响应式数据代理
---@field subscribe func

---返回一个响应式的数据代理对象
---@return IReactiveProxy
local function Reactive(input)
    local proxy = {}
    local mt = {
        
    }
    mt.__index = function(t, k)
        return rawget(input, k)
    end
    mt.__newindex = function(t, k, v)
        local oldV = rawget(input, k)
        if oldV ~= v then
            for _,callback in ipairs(mt._callbacks[k] or {}) do
                callback(v, oldV)
            end
        end
    end
    mt._callbacks = {}
    function proxy:subscribe(k, callback)
        if not mt._callbacks[k] then
            mt._callbacks[k] = {}
        end
        table.insert(mt._callbacks[k], callback)
    end

    setmetatable(proxy, mt)
    return proxy
end

return Reactive
